<template>
  <div class="flexContent">
    <div class="searchBox">

      <el-form ref="formRef" :model="sForm" class="demo-form-inline" label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="门店名称">
                  <el-select v-model="sForm.storeId" popper-class="eloption" clearable style="width: 95%;" filterable
                    placeholder="请选择" @click.native="getShopEasy">
                    <el-option v-for="store in storeList" :key="store.storeId" :value="store.storeId"
                      :label="store.storeName" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="支付状态">
                  <el-select v-model="sForm.payState" popper-class="eloption" style="width: 95%;" placeholder="请选择">
                    <el-option value="0" label="未付款" />
                    <el-option value="1" label="已付款" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="10" :xl="6">
                <el-form-item label="订单日期">
                  <el-date-picker clearable v-model="date" style="width: 95%;" type="daterange" unlink-panels
                    range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="btnBox">
            <el-button @click.stop="searchFn" class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button @click.stop="resetFn" class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>

    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" border class="tablebox" ref="xTable1"
          :row-config="{ isHover: true }" :scroll-y="{ enabled: true, gt: 21 }" style="width: 100%" height="98%"
          size="medium" align="center" :footer-method="getSummaries" show-footer>
          <vxe-column type="seq" width="80" title="序号" />
          <vxe-column field="storeName" title="门店名称" min-width="150" />
          <vxe-column field="cashNumber" title="现金支付编号" min-width="180" />
          <vxe-column field="cashMoney" title="订单金额" min-width="120" />
          <vxe-column field="cashDate" title="现金日期" width="120" />
          <vxe-column field="payState" title="支付状态" width="120">
            <template v-slot="{ row }">
              <span>{{ row.payState == '1' ? '已付款' : '未付款' }}</span>
            </template>
          </vxe-column>
          <vxe-column field="payTime" title="支付时间" width="180">
            <template v-slot="{ row }">
              <span v-if="row.payTime">{{ row.payTime | parseTime('YYYY-MM-DD HH:mm:ss') }}</span>
              <span v-else>无</span>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div>
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,->, sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>

<script>
import { getCashList, getShopListSelect } from "@/api/store";
export default {
  name: "IndependentAccount",
  data() {
    return {
      dialogTableVisible: false,
      sForm: {
        storeId: "",
        payState: '0',
      },
      storeList: [],
      accountList: [],
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
      date: [],
      isSearch: false,
      allDataArr: [],
      fieldSort: ['seq', 'storeName', 'cashNumber', 'cashMoney', 'cashDate', 'payState', 'payTime']
    };
  },
  computed: {
    startTime() {
      if (this.date == null) {
        return "";
      }
      if (this.date.length > 0) {
        return this.date[0];
      } else {
        return "";
      }
    },
    endTime() {
      if (this.date == null) {
        return "";
      }
      if (this.date.length > 0) {
        return this.date[1];
      } else {
        return "";
      }
    },
  },
  created() {
    this.getShopEasy();
    this.getCashList();
    // this.getAllCount();
  },
  methods: {
    // 获取门店列表
    async getShopEasy() {
      const res = await getShopListSelect();
      if (res.code === 200) {
        this.storeList = res.data;
      }
    },
    // 查找列表数据
    async getCashList() {
      const { startTime, endTime } = this;
      if (!this.isSearch) {
        this.sForm.storeId = "";
        this.sForm.payState = "0";
        this.data = [];
      }
      const res = await getCashList({
        ...this.page,
        ...this.sForm,
        startTime,
        endTime,
      });
      if (res.code === 200) {
        console.log(res.data, "data");
        this.accountList = res.data.pageInfo.list;
        this.page.total = res.data.pageInfo.total;
        const $table = this.$refs.xTable1
        let resArr = [];
        let allCountObj = {
          cashMoney: res.data.sum
        };
        this.allDataArr = [];
        this.fieldSort.forEach(e => {
          if (e == 'seq') {
            resArr.push('合计');
          } else if (allCountObj[e] == null) {
            resArr.push('-');
          } else {
            resArr.push(allCountObj[e]);
          }
        });
        this.allDataArr.push(resArr);
        $table.loadData(this.accountList);
      }
    },
    getSummaries() {
      return this.allDataArr;
    },
    // 搜索方法
    async searchFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearch = true;
      this.getCashList();
    },
    // 重置
    resetFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearch = false;
      this.sForm.storeId = "";
      this.sForm.payState = "0";
      this.data = [];
      this.getCashList();
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;

      this.getCashList();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      this.page.pageNum = val;
      this.getCashList();
    },
    close() {
      this.dialogTableVisible = false;
    },
  },
};
</script>

<style scoped lang="scss">
.inner {
  .item {
    p {
      display: flex;

      &:nth-child(1) {
        justify-content: space-between;
      }
    }
  }
}
</style>
